package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.dto.GateWayDto;
import com.c611.smartclassroom.mapper.BuildMapper;
import com.c611.smartclassroom.mapper.ClassRoomMapper;
import com.c611.smartclassroom.mapper.GateWayMapper;
import com.c611.smartclassroom.model.*;
import com.c611.smartclassroom.service.GateWayService;
import com.c611.smartclassroom.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class GateWayServiceImpl implements GateWayService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    GateWayMapper gateWayMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SchoolService schoolService;

    @Autowired
    ClassRoomMapper classRoomMapper;

    /**
     * 根据点击的教学楼Id，查询所有网关
     * @param buildId
     * @return
     */
    @Cacheable(value = "gateWay",key = "'queryGateWayByBuildId::' + #buildId + '::' + #status + '::' + #gateWayId",unless = "#result == null ")
    @Override
    public List<GateWayDto> queryGateWayByBuildId(String buildId, int status, String gateWayId) {
        List<ClassRoom> classRooms = null;
        //当没有指定教学楼id时默认查找全部。
       if("".equals(buildId)) {
           ClassRoomExample classRoomExample = new ClassRoomExample();
           classRoomExample.createCriteria().andDelflagEqualTo(0);
           classRooms = classRoomMapper.selectByExample(classRoomExample);
        }else {
           //通过教学楼查询所有的教室(调用了schoolService查询教室表)
           classRooms = schoolService.queryCalss(buildId);
       }
        //保存所有教室的网关
        List<GateWayDto> gateWayDtoList = new ArrayList<>();
        //查询所有教室的所有网关(查询网关表)
        for (ClassRoom classRoom : classRooms){
            GateWayExample gateWayExample = new GateWayExample();
            GateWayExample.Criteria criteria = gateWayExample.createCriteria();
            criteria.andClassroomidEqualTo(classRoom.getClassroomid()).andDelflagEqualTo(0);
            //当status有值时
            if (status != -1){
                criteria.andOnlinestatusEqualTo(status);
            }
            //当gateWayId有值时
            if (gateWayId != null){
                if (gateWayId.contains(":")){
                    //参数是网关gateWayMac
                    criteria.andGatewaymacLike("%" + gateWayId + "%");
                }else {
                    //参数是网关gateWayId
                    criteria.andGatewayidLike("%" + gateWayId + "%");
                }
            }
            List<GateWay> gateWayList = gateWayMapper.selectByExample(gateWayExample);
            //遍历该教室的网关
            for (GateWay gateWay : gateWayList){
                GateWayDto gateWayDto = new GateWayDto(gateWay.getGatewayid(),gateWay.getGatewaymac(),classRoom.getClassroom(),classRoom.getClassroomid(),gateWay.getEqnumber(),
                        gateWay.getActime(),gateWay.getOnlinestatus());
                gateWayDtoList.add(gateWayDto);
            }
        }

        return gateWayDtoList;
    }

    /**
     * 通过网关Id查询网关
     * @return
     */
    @Cacheable(value = "gateWay",key = "'queryGateWayByGateWayId::' + #gateWayId",unless = "#result == null ")
    @Override
    public GateWay queryGateWayByGateWayId(String gateWayId) {
        GateWay gateWay = gateWayMapper.selectByPrimaryKey(gateWayId);
        //查询是否置废
        if (gateWay != null && gateWay.getDelflag() != 1)
            return gateWay;
        return null;
    }

    /**
     * 通过教室Id查询网关
     * @param classroomId
     * @return
     */
    @Cacheable(value = "gateWay",key = "'queryGateWayByClassroomId::' + #classroomId",unless = "#result == null ")
    @Override
    public List<GateWay> queryGateWayByClassroomId(String classroomId) {
        GateWayExample gateWayExample = new GateWayExample();
        gateWayExample.createCriteria().andClassroomidEqualTo(classroomId).andDelflagEqualTo(0);
        return gateWayMapper.selectByExample(gateWayExample);
    }

    /**
     * 3.增加网关
     * @param gateWay
     * @return
     */
    @CacheEvict(value = "gateWay",allEntries = true)
    @Override
    public int addGateWay(GateWay gateWay) {
        //设置添加时的时间
        gateWay.setActime(new Date());
        return gateWayMapper.insertSelective(gateWay);
    }

    /**
     * 4.编辑网关
     * @param gateWay
     * @return
     */
    @CacheEvict(value = "gateWay",allEntries = true)
    @Override
    public int saveGateWay(GateWay gateWay) {
        return gateWayMapper.updateByPrimaryKeySelective(gateWay);
    }

    /**
     * 5.删除网关
     * @param gateWayId
     * @return
     */
    @CacheEvict(value = "gateWay",allEntries = true)
    @Override
    public int delGateWay(String gateWayId) {
        GateWay gateWay = queryGateWayByGateWayId(gateWayId);
        if (gateWay == null) return 0;
        gateWay.setDelflag(1);
        return saveGateWay(gateWay);
    }
}
